// 0 引入 用来发送请求的 方法 一定要把路径补全
import {
  request
} from '../../request/index.js'
//Page Object
Page({
  data: {
    //轮播图数据
    swiperList: [],
    //导航栏数据
    navList: [],
    //楼层数据
    floorList: []
  },
  //options(Object)
  onLoad: function (options) {
    // wx.request({
    //   url: 'https://api-hmugo-web.itheima.net/api/public/v1/home/swiperdata',
    //   success: (result)=>{
    //     console.log(result)
    //     this.setData({
    //       swiperList:result.data.message
    //     })
    //   }
    // });
    //获取轮播图数据
    this.getSwiperList();

    //获取导航数据
    this.getNavList();

    //获取楼层数据
    this.getFloorList()

  },

  //获取轮播图数据
  getSwiperList() {
    request({
      url: '/home/swiperdata'
    }).then(result => {
      result.forEach(v => v.navigator_url = v.navigator_url.replace(/main/, 'index'))
      console.log(result)
      this.setData({
        swiperList: result
      })
    })
  },
  //获取导航数据
  getNavList() {
    request({
      url: "/home/catitems"
    }).then(res => {
      console.log(res)
      res[0].navigator_url = res[0].navigator_url.replace(/main/, 'index')
      this.setData({
        navList: res
      })
    })
  },
  //获取楼层数据
  getFloorList() {
    request({
      url: '/home/floordata'
    }).then(res => {
      for (let i = 0; i < res.length; i++) {
        for (let j = 0; j < res[i].product_list.length; j++) {
          res[i].product_list[j].navigator_url = res[i].product_list[j].navigator_url.replace(/\?/, '/index?')
        }
      }
      console.log(res)
      this.setData({
        floorList: res
      })
    })
  },
  //下拉刷新触发事件
  onPullDownRefresh() {
    wx.showLoading({
      title: '加载中',
      duration: 1000,
      mask: true
    });
    //把每个数组清空
    this.setData({
      swiperList: [],
      navList: [],
      floorList: []
    })
    //重新发起请求
    setTimeout(() => {
      this.getSwiperList()
      this.getNavList()
      this.getFloorList()
      wx.stopPullDownRefresh() //停止下拉刷新
      wx.hideLoading();
    }, 1000)
  }
});